package com.xiongtian.sparsearay;

import java.io.*;
import java.util.Arrays;

/**
 * @author xiongtian
 * @version 1.0
 * @date 2021/2/15 16:42
 * <p>
 * 稀疏数组
 * 作业加入io写入磁盘
 */
public class SparseArray {
    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder();
        System.out.println(stringBuilder.toString());
        // 创建一个原始的二位数组 11 * 11
        // 0：表示没有棋子 1：黑子 2：蓝子
        int chessArr1[][] = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        // 输出原始的二维数组
        System.out.println("原始的二维数组：");
        for (int[] row : chessArr1) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }
        // 将二维数组 转 稀疏数组
        // 1. 遍历二维数组得到非 0 数据的个数
        int sum = 0;
        for (int[] row : chessArr1) {
            for (int data : row) {
                if (data != 0) {
                    sum++;
                }
            }
        }
        System.out.println(sum);
        // 2. 创建对应的稀疏数组
        int sparseArr[][] = new int[sum + 1][3];
        // 3. 给稀疏数组赋值
        sparseArr[0][0] = chessArr1.length;
        sparseArr[0][1] = chessArr1[0].length;
        sparseArr[0][2] = sum;
        // 4. 给稀疏数组赋值
        // 计数器
        int count = 1;
        for (int i = 0; i < chessArr1.length; i++) {
            for (int j = 0; j < chessArr1[i].length; j++) {
                if (chessArr1[i][j] != 0) {
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArr1[i][j];
                    count++;
                }
            }
        }

        System.out.println();
        System.out.println("得到的稀疏数组为：");
        for (int i = 0; i < sparseArr.length; i++) {
            System.out.printf("%d\t%d\t%d\t\n", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);
        }
        // 将稀疏数组 恢复 原始的二维数组
        // 1. 读取稀疏数组
        int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];

        // 2. 遍历将值写入
        for (int i = 1; i < sparseArr.length; i++) {
            chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }
        // 输出恢复后的二维数组
        System.out.println();
        System.out.println("恢复后的二维数组");
        printArr(chessArr2);
        System.out.println("测试");
        System.out.println(arrToString(chessArr2));
        String path = "D:\\xiongtian\\map.data";
        writeArrsToFile(sparseArr, path);
        System.out.println("从文件中读取的数组");
        printArr(readArrsFromFile(path));
    }

    /**
     * 打印二维数组
     */
    public static void printArr(int[][] arrs) {
        if (null == arrs || arrs.length < 0) {
            return;
        }
        for (int[] row : arrs) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }
    }

    public static String arrToString(int[][] arrs) {
        StringBuilder arrString = new StringBuilder();
        if (null == arrs || arrs.length < 0) {
            return arrString.toString();
        }
        for (int[] row : arrs) {
            for (int data : row) {
                arrString.append(data + "\t");
            }
            arrString.append("\n");
        }
        return arrString.toString();
    }

    /**
     * 将数组写入到指定的文件中去
     *
     * @param arrs
     * @param filePath
     */
    public static void writeArrsToFile(int[][] arrs, String filePath) {
        File file = new File(filePath);
        InputStream is = null;
        BufferedWriter bufferedWriter = null;

        try {
            bufferedWriter = new BufferedWriter(new FileWriter(file));

            if (!file.exists()) {
                file.createNewFile();
            }
            String arrToString = arrToString(arrs);
            bufferedWriter.write(arrToString);
            bufferedWriter.flush();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedWriter != null) {
                    bufferedWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 从文件中将二维数组读取出来
     * @param path
     * @return
     */
    public static int[][] readArrsFromFile(String path) {
        File file = new File(path);
        BufferedReader bufferedReader = null;
        int[][] sparseArr = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(file));
            if (!file.exists()) {
                return null;
            }
            String row = bufferedReader.readLine();
            String[] rows = row.split("\t");
            sparseArr = new int[Integer.parseInt(rows[2]) + 1][rows.length];
            for (int i = 0; i < rows.length; i++) {
                sparseArr[0][i] = Integer.parseInt(rows[i]);
            }
            int index = 1;
            while ((row = bufferedReader.readLine()) != null) {
                String[] tempArrs = row.split("\t");
                for (int i = 0; i < rows.length; i++) {
                    sparseArr[index][i] = Integer.parseInt(tempArrs[i]);
                }
                index++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != bufferedReader) {
                    bufferedReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sparseArr;
    }

}
